<?php
// +----------------------------------------------------------------------
// | [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Author 荒年 < 931274989@qq.com >
// +----------------------------------------------------------------------
// | Explain 云盟洗车后台
// +----------------------------------------------------------------------
namespace app\yunmeng\controller;

use app\base\controller\Base;
use app\yunmeng\model\YmXiche as ymxicheModel;
use app\yunmeng\model\YmCarlog as YmCasrlogModel;
use think\Db;

class Ymxiche extends Base
{
  public function lst()
  {
    $model = new ymxicheModel();
    $post = $this->request->param();
    $xiche = empty($post['name']) ? $model->paginate(20) : Db::name('ym_xiche')->whereLike('name','%'.$post['name'].'%')->paginate(20,false,['query'=>$this->request->param()]);
    $this->assign('xiche',$xiche);
    return view();
  }

  public function addEdit()
  {
    //获取管理员id
    $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    $model = new ymxicheModel();
    if($id > 0) {
      //是修改操作
      if($this->request->isPost()) {
        //提交操作
        $post = $this->request->post();
        if(false == $model->allowField(true)->save($post,['id'=>$id])) {
          return $this->error('修改失败');
        } else {
          return $this->success('修改洗车行信息成功','yunmeng/ymxiche/lst');
        }
      } else {
        //非提交操作
        $info = $model->where('id',$id)->find();
        $this->assign('info',$info);
        return $this->fetch();
      }
    } else {
      //是新增操作
      if($this->request->isPost()) {
        //是提交操作
        $post = $this->request->post();
        $post['hexiao'] = rand(100000,999999);
        //验证部分数据合法性
        if(false == $model->allowField(true)->save($post)) {
          return $this->error('添加加盟商失败');
        } else {
          return $this->success('添加加盟成功','yunmeng/ymxiche/lst');
        }
      } else {
        return $this->fetch();
      }
    }
  }

  public function del($id)
  {
    if($id > 0){
      if(false == Db::name('ym_xiche')->where('id','=',$id)->delete()){
        $this->error('删除失败');
      } else {
        $this->success('删除成功','yunmeng/ymxiche/lst');
      }
    } else {
      $this->error('ID不正确');
    }
  }

  public function carlog()
  {
    $model = new YmCasrlogModel();
    $logs = $model::with('cars')->paginate(20);
    $this->assign('logs',$logs);
    return view();
  }

  // 洗车日志删除
  public function delete()
  {
    $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    if($id > 0){
      if(false == Db::name('ym_carlog')->where('id','=',$id)->delete()){
        $this->error('删除失败');
      } else {
        $this->success('删除成功','yunmeng/ymxiche/carlog');
      }
    } else {
      $this->error('ID不正确');
    }
  }

}

?>